from librpg.config import game_config, graphics_config
from librpg.menu import (Menu, Label, Cursor)
from librpg.input import Input


class ExitLabel(Label):

    def __init__(self):
        Label.__init__(self, 'Exit')

    def activate(self):
        self.menu.close()
        return True


class AdvanceLabel(Label):

    def __init__(self):
        Label.__init__(self, 'Advance')

    def activate(self):
        self.menu.advance()
        return True


class TeamMenu(Menu):

    def __init__(self, simulation):
        Menu.__init__(self, graphics_config.screen_width,
                      graphics_config.screen_height)
        self.simulation = simulation
        
        y = 20
        Y_SEPARATION = 40
        
        x = 20
        l = ExitLabel()
        self.add_widget(l, (x, y))
        
        y += Y_SEPARATION
        l = AdvanceLabel()
        self.add_widget(l, (x, y))
        
        y += Y_SEPARATION
        txt = 'My team is %s' % self.simulation.player_team_name
        self.my_team_label = Label(txt, focusable=False)
        self.add_widget(self.my_team_label, (x, y))
        
        y += Y_SEPARATION
        self.opponent_label = Label(text='', focusable=False)
        self.add_widget(self.opponent_label, (x, y))
        
        y += Y_SEPARATION
        self.last_game_label = Label(text='', focusable=False)
        self.add_widget(self.last_game_label, (x, y))
        
        y += Y_SEPARATION
        self.records_label = Label(text='', focusable=False)
        self.add_widget(self.records_label, (x, y))
        
        y += Y_SEPARATION
        self.date_label = Label(text='', focusable=False)
        self.add_widget(self.date_label, (x, y))
        
        c = Cursor()
        c.bind(self)
        
        self.update_next_game()
        self.update_standings()
        self.update_date()

    def update_input(self):
        for k in game_config.key_cancel:
            Input.was_pressed(k)

    def advance(self):
        sim = self.simulation
        sim.advance()
        
        self.update_last_game()
        self.update_next_game()
        self.update_standings()
        self.update_date()

    def update_next_game(self):
        txt = 'Next game against %s' % self.simulation.challenger.name
        self.opponent_label.text = txt

    def update_last_game(self):
        txt = 'Last game: %s' % self.simulation.last_match
        self.last_game_label.text = txt

    def update_standings(self):
        sim = self.simulation
        txt = 'Records: %s' % sim.standings[sim.player_team_name]
        self.records_label.text = txt

    def update_date(self):
        self.date_label.text = str(self.simulation.calendar)
